package com.kg.sdk;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AppOpsManager;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Binder;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
import android.util.Log;

import com.common.network.NetworkConfigData;
import com.common.network.NetworkResponse;
import com.common.network.RequestListener;
import com.example.kpgamesdk.R;
import com.kg.sdk.activity.KPLoginActivity;
import com.kg.sdk.activity.KPUserCenterActivity;
import com.kg.sdk.dlg.KPChargeDlg;
import com.kg.sdk.dlg.KPPermissionDlg;
import com.kg.sdk.network.KPPostRequest;
import com.kg.sdk.util.CommonUtil;
import com.kg.sdk.util.KPNetworkData;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import static android.content.ContentValues.TAG;
import static android.content.Context.ACTIVITY_SERVICE;

public class KPGameSdk {
    public static int ERROR = -1;
    public static int LOGIN_ERROR = -2;
    public static boolean isLogin = false;
    public static String pleaseLogin = "请先登录";
    public static void showFloatView2(Context context){
        if(isRunService(context)){
            Intent intent = new Intent(KPNetworkData.SHOW_FLOAT_VIEW);
            context.sendBroadcast(intent);
        }else{
            startFloatService(context);
        }
    }

    public static void showFloatView(Activity activity){
        if(checkFloatPermission1(activity)){
            showFloatView2(activity);
        }else{
            if (Build.VERSION.SDK_INT >= 23) {//6.0以上
                KPPermissionDlg kpPermissionDlg = new KPPermissionDlg(activity);
                kpPermissionDlg.showDialog();
                /*
                Intent intent = new Intent();
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                intent.setData(Uri.fromParts("package", activity.getPackageName(), null));
                activity.startActivityForResult(intent, CHECKPRESSMISSON_CODE);*/
            }else{
            }
        }
    }

    public static void hideFloatView(Context context){
       // Intent intent = new Intent(KPNetworkData.HIDE_FLOAT_VIEW);
      //  context.sendBroadcast(intent);
        stopFloatService(context);
    }

    public static void showFloatView1(Context context){
        Intent intent = new Intent(KPNetworkData.SHOW_FLOAT_VIEW);
        context.sendBroadcast(intent);

    }

    public static void hideFloatView1(Context context){
         Intent intent = new Intent(KPNetworkData.HIDE_FLOAT_VIEW);
          context.sendBroadcast(intent);
    }

    public static void startFloatService(Context context){
        Intent intent = new Intent(context, KPSDKGameMenuServer.class);
        context.startService(intent);
    }

    public static void stopFloatService(Context context){
        Intent intent = new Intent(context,KPSDKGameMenuServer.class);
        context.stopService(intent);
    }

    public static boolean isLogin(){
        return  isLogin;
    }
    public static  boolean isLogin(Context context){
        SharedPreferencesCtrl sharedPreferencesCtrl = new SharedPreferencesCtrl(context,KPNetworkData.KPGAME);
        boolean is = sharedPreferencesCtrl.getBoolean(KPNetworkData.IS_LOGIN_KEY,false);
        return is;
    }
    public static void startLogin1(Activity activity){
        if(isLogin()){
            CommonUtil.showToast(activity,"已经登录了");
            return;
        }
        startLogin2( activity);
    }
    public static void startLogin2(Activity activity){
        Intent intent = new Intent();
        intent.setClass(activity, KPLoginActivity.class);
        activity.startActivityForResult(intent,KPNetworkData.LOGIN_REQUEST);
    }

    public static void startLogin(Activity activity){
        if(isLogin(activity)){
            CommonUtil.showToast(activity,"已经登录了");
            return;
        }
        startLogin2( activity);
    }

    public static void exitLogin(Context context){
        /*
        SharedPreferencesCtrl sharedPreferencesCtrl = new SharedPreferencesCtrl(context,KPNetworkData.KPGAME);
        sharedPreferencesCtrl.put(KPNetworkData.IS_LOGIN_KEY,false);
        hideFloatView(context);
        isLogin = false;
        CommonUtil.showToast(context,"注销成功");
         */
        exitLogin(context,true);
    }
    public static void exitLogin(Context context,boolean isCleanCache){
        SharedPreferencesCtrl sharedPreferencesCtrl = new SharedPreferencesCtrl(context,KPNetworkData.KPGAME);
        sharedPreferencesCtrl.put(KPNetworkData.IS_LOGIN_KEY,false);
        if(isCleanCache){
            sharedPreferencesCtrl.put(KPNetworkData.ACCOUNT,"");
            sharedPreferencesCtrl.put(KPNetworkData.newpwd,"");
        }
        hideFloatView(context);
        isLogin = false;
        CommonUtil.showToast(context,"注销成功");
    }
    public final static String serviceName = "com.kg.sdk.KPSDKGameMenuServer";
    private static boolean isRunService(Context context) {
        ActivityManager manager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceName.equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }

   

    public static void startAutostartSetting(Context context){
      
    }
    public static void startUserCenter1(Context context){//启动用户中心
        if(!isLogin()){
            CommonUtil.showToast(context,pleaseLogin);
            return;
        }
        startUserCenter2(context);
    }
    public static void startUserCenter2(Context context){//启动用户中心
        Intent intent = new Intent();
        intent.setClass(context, KPUserCenterActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    public static void startUserCenter(Context context){//启动用户中心
        if(!isLogin(context)){
            CommonUtil.showToast(context,pleaseLogin);
            return;
        }
        startUserCenter2(context);
    }

    //获取手机类型
    private static String getMobileType() {
        return Build.MANUFACTURER;
    }
    //跳转至授权页面
    public static void jumpStartInterface(Context context) {
        Intent intent = new Intent();
        try {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            Log.e("HLQ_Struggle", "******************当前手机型号为：" + getMobileType());
            ComponentName componentName = null;
            if (getMobileType().equals("Xiaomi")) { // 红米Note4测试通过
                componentName = new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity");

            } else if (getMobileType().equals("Letv")) { // 乐视2测试通过
                intent.setAction("com.letv.android.permissionautoboot");
            } else if (getMobileType().equals("samsung")) { // 三星Note5测试通过
                //componentName = new ComponentName("com.samsung.android.sm_cn", "com.samsung.android.sm.ui.ram.AutoRunActivity");
                //componentName = ComponentName.unflattenFromString("com.samsung.android.sm/.ui.ram.RamActivity");// Permission Denial not exported from uid 1000，不允许被其他程序调用
                componentName = ComponentName.unflattenFromString("com.samsung.android.sm/.app.dashboard.SmartManagerDashBoardActivity");
            } else if (getMobileType().equals("HUAWEI")) { // 华为测试通过
                //componentName = new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity");//锁屏清理
                componentName = ComponentName.unflattenFromString("com.huawei.systemmanager/.startupmgr.ui.StartupNormalAppListActivity");//跳自启动管理
                //SettingOverlayView.show(context);
            } else if (getMobileType().equals("vivo")) { // VIVO测试通过
                componentName = ComponentName.unflattenFromString("com.iqoo.secure/.safeguard.PurviewTabActivity");
            } else if (getMobileType().equals("Meizu")) { //万恶的魅族
                //componentName = ComponentName.unflattenFromString("com.meizu.safe/.permission.PermissionMainActivity");//跳转到手机管家
                componentName = ComponentName.unflattenFromString("com.meizu.safe/.permission.SmartBGActivity");//跳转到后台管理页面
            } else if (getMobileType().equals("OPPO")) { // OPPO R8205测试通过
                componentName = ComponentName.unflattenFromString("com.oppo.safe/.permission.startup.StartupAppListActivity");
            } else if (getMobileType().equals("ulong")) { // 360手机 未测试
                componentName = new ComponentName("com.yulong.android.coolsafe", ".ui.activity.autorun.AutoRunListActivity");
            } else {
                // 将用户引导到系统设置页面
                if (Build.VERSION.SDK_INT >= 9) {
                    Log.e("HLQ_Struggle", "APPLICATION_DETAILS_SETTINGS");
                    intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                    intent.setData(Uri.fromParts("package", context.getPackageName(), null));
                } else if (Build.VERSION.SDK_INT <= 8) {
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
                    intent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
                }
            }
            intent.setComponent(componentName);
            context.startActivity(intent);
            if (getMobileType().equals("Xiaomi")) {
                //showtip();//显示弹窗（**特别注意**）
            }
            if (getMobileType().equals("samsung")){
             //   new SettingOverlayView().show(context);//显示悬浮窗
            }

        } catch (Exception e) {//抛出异常就直接打开设置页面
            Log.e("HLQ_Struggle", e.getLocalizedMessage());
            intent = new Intent(Settings.ACTION_SETTINGS);
            context.startActivity(intent);
        }
    }

    private static void charge2(final Context context, String orderno, String product, String role_id, float amount, String server_id, final KPGameListener listener){
        KPGameData kpGameData = getGameData(context);
        if(kpGameData.game_id == -1){
            CommonUtil.showToast(context,"请先配置游戏id");
            return;
        }
        final  String loginError = "登录失效,请用重新登录";
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("orderno",orderno);
            jsonObject.put("pay_type",3);
            //  jsonObject.put("game_id",game_id);
            jsonObject.put("product",product);
            jsonObject.put("role_id",role_id);
            jsonObject.put("amount",amount);
            jsonObject.put("server_id",server_id);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        RequestListener requestListener = new RequestListener() {
            @Override
            public void onSuccess(NetworkResponse result) {
                int code = result.getInt(KPNetworkData.ERROR_CODE);
                String data = result.getString(NetworkConfigData.DATA_KEY);
                Handler handler = new Handler(Looper.getMainLooper());
                int is_login = result.getInt("is_login");
                Log.e("data"," usce center2 logni success is "+data+" isLogin is "+is_login+" code is "+code);
                if(is_login == 0){
                    if(listener != null){
                        listener.onFail(LOGIN_ERROR,loginError);
                    }
                    Runnable runnable = new Runnable() {
                        @Override
                        public void run() {
                            if(kpChargeDlg != null){
                                kpChargeDlg.closeWaitDlg();
                            }
                            CommonUtil.showToast(context,loginError);
                        }
                    };
                    handler.post(runnable);
                    return;
                }
                if(code == KPNetworkData.SUCCESS){
                    Runnable runnable = new Runnable() {
                        @Override
                        public void run() {
                            if(kpChargeDlg != null){
                                kpChargeDlg.dismiss();
                            }
                        }
                    };
                    handler.post(runnable);
                    try {
                        JSONObject jsonData = new JSONObject(data);
                        JSONObject jsonData1 = new JSONObject();
                        jsonData1.put("orderno",jsonData.getString("orderno"));
                        jsonData1.put("amount",jsonData.getString("amount"));
                        jsonData1.put("product",jsonData.getString("product"));
                        jsonData1.put("pay_status",jsonData.getString("pay_status"));
                        Log.e("ch"," jsonData1 is "+jsonData1.toString());
                        if(listener != null){
                            listener.onSuccess(jsonData1);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }else{
                    final String errorMsg = result.getString(KPNetworkData.MSG_KEY);
                    Log.e("data"," errorMsg is "+errorMsg);
                    if(listener != null){
                        listener.onFail(ERROR,errorMsg);
                    }
                    Runnable runnable1 = new Runnable() {
                        @Override
                        public void run() {
                            if(kpChargeDlg != null){
                                kpChargeDlg.closeWaitDlg();
                            }
                            CommonUtil.showToast(context,errorMsg);
                        }
                    };
                    handler.post(runnable1);
                    // showError(errorMsg);
                }
            }

            @Override
            public void onError(NetworkResponse error) {
                final String errorMsg = error.getString(NetworkConfigData.MESSAGE_KEY);
                //  showError(errorMsg);
                Log.e("data"," login errorMsg is "+errorMsg);
                Handler handler = new Handler(Looper.getMainLooper());
                Runnable runnable = new Runnable() {
                    @Override
                    public void run() {
                        if(kpChargeDlg != null){
                            kpChargeDlg.closeWaitDlg();
                        }
                        CommonUtil.showToast(context,errorMsg);
                    }
                };
                handler.post(runnable);
                if(listener != null){
                    listener.onFail(ERROR,errorMsg);
                }

            }
        };
        SharedPreferencesCtrl sharedPreferencesCtrl = new SharedPreferencesCtrl(context,KPNetworkData.KPGAME);
        String auth_code = sharedPreferencesCtrl.getString(KPNetworkData.AUTH_CODE_KYE,"");
        String auth_key = sharedPreferencesCtrl.getString(KPNetworkData.AUTH_KYE,"");
        KPPostRequest postRequest = new KPPostRequest(context,KPNetworkData.USER_CHARGE,jsonObject.toString(),requestListener);
        postRequest.header("auth_code",auth_code);
        postRequest.header("auth_key",auth_key);
        postRequest.startAsync();
    }
    public static void  charge(final Context context, final String orderno, final String product, final String role_id, final float amount, final String server_id, final KPGameListener listener){//充值接口
        if(!isLogin(context)){
            CommonUtil.showToast(context,pleaseLogin);
            return;
        }
       final KPChargeDlg.OKClickListener okClickListener = new KPChargeDlg.OKClickListener() {
            @Override
            public void okClick() {
                charge2(context,orderno,product,role_id,amount,server_id,listener);
            }
        };
        //  charge2(context,orderno,product,role_id,amount,server_id,listener);

        String url = KPNetworkData.USER_CENTER;
        RequestListener listener1 = new RequestListener() {
            @Override
            public void onSuccess(NetworkResponse result) {
                int code = result.getInt(KPNetworkData.ERROR_CODE);
                final String data = result.getString(NetworkConfigData.DATA_KEY);
                Log.e("data"," usce center logni success is "+data);
                if(code == KPNetworkData.SUCCESS){
                    JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(data);
                        final String surplus = jsonObject.getString(KPNetworkData.SURPLUS);
                        final String account = jsonObject.getString(KPNetworkData.ACCOUNT);
                        Handler handler = new Handler(Looper.getMainLooper());
                        Runnable runnable = new Runnable() {
                            @Override
                            public void run() {
                                kpChargeDlg = new KPChargeDlg(context,okClickListener);
                                kpChargeDlg.setChargeKB(""+amount);
                                kpChargeDlg.setUseKB(surplus);
                                kpChargeDlg.setChargeName(product);
                                kpChargeDlg.setAccount(account);
                                kpChargeDlg.showDialog();
                            }
                        };
                        handler.post(runnable);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }else{
                    final String errorMsg = result.getString(KPNetworkData.MSG_KEY);
                    Log.e("data"," login errorMsg 11 is "+errorMsg);
                    if(listener != null){
                        listener.onFail(ERROR,errorMsg);
                    }
                }
            }

            @Override
            public void onError(NetworkResponse error) {
                final String errorMsg = error.getString(NetworkConfigData.MESSAGE_KEY);
                if(listener != null){
                    listener.onFail(ERROR,errorMsg);
                }
            }
        };
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put(KPNetworkData.ACT_KEY,"index");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        KPPostRequest postRequest = new KPPostRequest(context,url,jsonObject.toString(),listener1);
        SharedPreferencesCtrl sharedPreferencesCtrl = new SharedPreferencesCtrl(context,KPNetworkData.KPGAME);
        String auth_code = sharedPreferencesCtrl.getString(KPNetworkData.AUTH_CODE_KYE,"");
        String auth_key = sharedPreferencesCtrl.getString(KPNetworkData.AUTH_KYE,"");
        postRequest.header("auth_code",auth_code);
        postRequest.header("auth_key",auth_key);
        postRequest.startAsync();

    }
    private static KPChargeDlg kpChargeDlg;
    public static void  charge1(final Context context, final String orderno, final String product,final String role_id,final float amount, final String server_id, final KPGameListener listener){//充值接口
        if(!isLogin()){
            CommonUtil.showToast(context,pleaseLogin);
            return;
        }
      //  charge2(context,orderno,product,role_id,amount,server_id,listener);
       final KPChargeDlg.OKClickListener okClickListener = new KPChargeDlg.OKClickListener() {
            @Override
            public void okClick() {
                charge2(context,orderno,product,role_id,amount,server_id,listener);
            }
        };

        String url = KPNetworkData.USER_CENTER;
        RequestListener listener1 = new RequestListener() {
            @Override
            public void onSuccess(NetworkResponse result) {
                int code = result.getInt(KPNetworkData.ERROR_CODE);
                final String data = result.getString(NetworkConfigData.DATA_KEY);
                Log.e("data"," usce center logni success is "+data);
                if(code == KPNetworkData.SUCCESS){
                    JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(data);
                        final String surplus = jsonObject.getString(KPNetworkData.SURPLUS);
                        Handler handler = new Handler(Looper.getMainLooper());
                        Runnable runnable = new Runnable() {
                            @Override
                            public void run() {
                                kpChargeDlg = new KPChargeDlg(context,okClickListener);
                                kpChargeDlg.setChargeKB(""+amount);
                                kpChargeDlg.setUseKB(surplus);
                                kpChargeDlg.setChargeName(product);
                                kpChargeDlg.showDialog();
                            }
                        };
                        handler.post(runnable);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }else{
                    final String errorMsg = result.getString(KPNetworkData.MSG_KEY);
                    Log.e("data"," login errorMsg 11 is "+errorMsg);
                    if(listener != null){
                        listener.onFail(ERROR,errorMsg);
                    }
                }
            }

            @Override
            public void onError(NetworkResponse error) {
                final String errorMsg = error.getString(NetworkConfigData.MESSAGE_KEY);
                if(listener != null){
                    listener.onFail(ERROR,errorMsg);
                }
            }
        };
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put(KPNetworkData.ACT_KEY,"index");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        KPPostRequest postRequest = new KPPostRequest(context,url,jsonObject.toString(),listener1);
        SharedPreferencesCtrl sharedPreferencesCtrl = new SharedPreferencesCtrl(context,KPNetworkData.KPGAME);
        String auth_code = sharedPreferencesCtrl.getString(KPNetworkData.AUTH_CODE_KYE,"");
        String auth_key = sharedPreferencesCtrl.getString(KPNetworkData.AUTH_KYE,"");
        postRequest.header("auth_code",auth_code);
        postRequest.header("auth_key",auth_key);
        postRequest.startAsync();

        // kpChargeDlg.showDialog();
    }


    public static KPUserData getKPUserData(Context context){
        if(!isLogin(context)){
            return  null;
        }
        return getKPUserData2(context);
    }
    public static KPUserData getKPUserData1(Context context){
        if(!isLogin()){
            return  null;
        }
        return getKPUserData2(context);
    }
    public static KPUserData getKPUserData2(Context context){
        SharedPreferencesCtrl sharedPreferencesCtrl = new SharedPreferencesCtrl(context,KPNetworkData.KPGAME);
        String auth_code = sharedPreferencesCtrl.getString(KPNetworkData.AUTH_CODE_KYE,"");
        String auth_key = sharedPreferencesCtrl.getString(KPNetworkData.AUTH_KYE,"");
        String user_code = sharedPreferencesCtrl.getString(KPNetworkData.USER_CODE,"");
        KPUserData kpUserData = new KPUserData();
        kpUserData.auth_code = auth_code;
        kpUserData.auth_key = auth_key;
        kpUserData.use_code = user_code;
        int channel_id = -1;
        int game_id = -1;
        KPGameData kpGameData = getGameData(context);
        kpUserData.game_id = kpGameData.game_id;
        kpUserData.channel_id = kpGameData.channel_id;
        return kpUserData;
    }


    private static boolean getAppOps(Context context) {
        try {
            Object object = context.getSystemService(context.APP_OPS_SERVICE);
            if (object == null) {
                return false;
            }
            Class localClass = object.getClass();
            Class[] arrayOfClass = new Class[3];
            arrayOfClass[0] = Integer.TYPE;
            arrayOfClass[1] = Integer.TYPE;
            arrayOfClass[2] = String.class;
            Method method = localClass.getMethod("checkOp", arrayOfClass);
            if (method == null) {
                return false;
            }
            Object[] arrayOfObject1 = new Object[3];
            arrayOfObject1[0] = Integer.valueOf(24);
            arrayOfObject1[1] = Integer.valueOf(Binder.getCallingUid());
            arrayOfObject1[2] = context.getPackageName();
            int m = ((Integer) method.invoke(object, arrayOfObject1)).intValue();
            return m == AppOpsManager.MODE_ALLOWED;
        } catch (Exception e) {
           // Log.e(TAG, "permissions judge: -->" + e.toString());
        }
        return false;
    }

    public static  boolean checkFloatPermission1(Context context) {
        if (Build.VERSION.SDK_INT >= 23) {//6.0以上
            boolean result = false;
            try {
                Class clazz = Settings.class;
                Method canDrawOverlays = clazz.getDeclaredMethod("canDrawOverlays", Context.class);
                result = (boolean) canDrawOverlays.invoke(null, context);
                Log.e(TAG, "checkFloatPermission:-->" + result);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4-5.1
            return getAppOps(context);
        } else {//4.4以下
            return true;
        }
    }
    public final static int CHECKPRESSMISSON_CODE = 1000;
    public static  void  checkFloatPermission(Activity activity) {
        if(checkFloatPermission1(activity)){

        }else{
            if (Build.VERSION.SDK_INT >= 23) {//6.0以上
                KPPermissionDlg kpPermissionDlg = new KPPermissionDlg(activity);
                kpPermissionDlg.showDialog();
                /*
                Intent intent = new Intent();
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                intent.setData(Uri.fromParts("package", activity.getPackageName(), null));
                activity.startActivityForResult(intent, CHECKPRESSMISSON_CODE);*/
            }else{

            }

        }
    }

    public static void checkFloatPermissionOnActivityResult(Activity activity,int requestCode, int resultCode, Intent data){
        if (requestCode == KPGameSdk.CHECKPRESSMISSON_CODE) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (Settings.canDrawOverlays(activity)) {
                   // showFloatView(activity);
                    showFloatView2(activity);
                } else {
                    //提示用户需要开启
                    //   CommonUtil.showToast(this,"悬浮窗权限未开启");
                }
            }
        }
    }

    public static KPGameData getGameData1(Context activity){

        AssetManager assetManager = activity.getAssets();
        KPGameData kpGameData = new KPGameData();
        try {
            InputStream ims =  assetManager.open("channel.txt");
            BufferedReader bf = new BufferedReader(new InputStreamReader(ims));
            String channelIdStr = bf.readLine();
            String gameIdStr = bf.readLine();
            String channelList[] = channelIdStr.split("=");
            String gameList[] = gameIdStr.split("=");
            kpGameData.channel_id = Integer.parseInt(channelList[1].trim());
            kpGameData.game_id = Integer.parseInt(gameList[1].trim());
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.e("kp"," kpGameData.channel_id is "+kpGameData.channel_id+" kpGameData.game_id is "+kpGameData.game_id);
        return  kpGameData;
    }
    public static KPGameData getGameData(Context activity){
        KPGameData kpGameData = new KPGameData();
        String channel = null;
        ApplicationInfo appinfo = activity.getApplicationInfo();
        final String channel_start_flag = "META-INF/channelId_";
        final String game_start_flag = "META-INF/gameId_";
        String channelId = "";
        String gameId = "";
        String sourceDir = appinfo.sourceDir;
        ZipFile zipfile = null;
        try {
            zipfile = new ZipFile(sourceDir);
            Enumeration<?> entries = zipfile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = ((ZipEntry) entries.nextElement());
                String entryName = entry.getName();
                if (entryName.startsWith(channel_start_flag)) {
                    channelId = entryName.replace(channel_start_flag,"");
                }
                if(entryName.startsWith(game_start_flag)){
                    gameId = entryName.replace(game_start_flag,"");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(channelId.length() > 0){
            kpGameData.channel_id = Integer.parseInt(channelId);
        }

        if(gameId.length() > 0){
            kpGameData.game_id = Integer.parseInt(gameId);
        }
      //  kpGameData.game_id = 79;
     //   kpGameData.channel_id = 999;
        Log.e("kp"," kpGameData.channel_id is "+kpGameData.channel_id+" kpGameData.game_id is "+kpGameData.game_id);
        return  kpGameData;
    }
    private void getUseInfo(Context context){

    }

    private static void  getFileContent(Context context) {
        ApplicationInfo appinfo = context.getApplicationInfo();
        String sourceDir = appinfo.sourceDir;
        ZipFile zipfile = null;
        try {
            zipfile = new ZipFile(sourceDir);
            Enumeration<?> entries = zipfile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = ((ZipEntry) entries.nextElement());
                String entryName = entry.getName();

                if (entryName.startsWith("META-INF/xxx")) { //xxx 表示要读取的文件名
                    //利用ZipInputStream读取文件
                    long size = entry.getSize();
                    if (size > 0) {
                        BufferedReader br = new BufferedReader(new InputStreamReader(zipfile.getInputStream(entry)));
                        String line;
                        while ((line = br.readLine()) != null) {  //文件内容都在这里输出了，根据你的需要做改变
                            System.out.println(line);
                        }
                        br.close();
                    }
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (zipfile != null) {
                try {
                    zipfile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void saveUserInfo(Context context,String account,String pwd){
        SharedPreferencesCtrl sharedPreferencesCtrl = new SharedPreferencesCtrl(context,KPNetworkData.KPGAME);
        sharedPreferencesCtrl.put(KPNetworkData.ACCOUNT,account);
        sharedPreferencesCtrl.put(KPNetworkData.newpwd,pwd);
    }
}
